package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.SDeliveryFeeService;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.SDeliveryFee;
import cn.huiyunche.base.service.vo.SOrderFeeDetailVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

@Service
public class SDeliveryFeeServiceImpl implements SDeliveryFeeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SDeliveryFeeServiceImpl.class);

    @Autowired
    private BFeeTypeService bFeeTypeService;

    @Autowired
    private BFeeModelService bFeeModelService;

    @Override
    public SOrderFeeDetailVo queryDeliveryFee(Boolean isDeliv, Integer vehicleSum) {
        LOGGER.info("queryDeliveryFee param : {}", isDeliv);
        if (null == isDeliv) {
            isDeliv = false;
        }
        if (null == vehicleSum) {
            vehicleSum = 1;
        }

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.INTERCITY_ORDER).getId(), Arrays.asList(BFeeTypeEnum.DELIVERY.getValue())).get(0);

        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());

        if (isDeliv) {
            //查询交车费
            SDeliveryFee deliveryFee = bFeeTypeService.getDeliveryFeeByFeeCode(BFeeTypeEnum.DELIVERY);
            vo.setCost(deliveryFee.getPrice().multiply(BigDecimal.valueOf(vehicleSum)).setScale(0, RoundingMode.UP).setScale(2));
        }

        return vo;
    }
}
